package clustering;

import static utils.Configuration.*;
import static utils.Configuration.setAttribute;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import preprocess.XMLDoc;
import similarity.Similarity;
import utils.CategoriesInfo;

public abstract class Clustering
{
    protected static int MAX_CLUSTER_TIMES;
    protected Similarity similarity;

    static
    {
        String times = CLUSTERING.MaxClusterTimes;
        try
        {
            MAX_CLUSTER_TIMES = Integer.parseInt(times);
        }
        catch (Exception e)
        {
            setAttribute("clustering", "max_cluster_times", "10");
            MAX_CLUSTER_TIMES = 10;
        }
    }

    public Clustering(Similarity similarity)
    {
        this.similarity = similarity;
    }

    /**
     * cluster the given XMLDoc set by the specified cluster count
     * 
     * @param list
     * @param count
     * @return
     */
    public abstract List<List<XMLDoc>> cluster(List<XMLDoc> list, int count);

    public CategoriesInfo toCategoriesInfo(List<List<XMLDoc>> lists)
    {
        HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();
        for (int i = 0; i < lists.size(); i++)
        {
            HashSet<String> set = new HashSet<String>();
            for (XMLDoc doc : lists.get(i))
            {
                set.add(doc.getDocID());
            }
            map.put("" + (i + 1), set);
        }
        CategoriesInfo result = CategoriesInfo.createCategoriesInfo(map);
        return result;
    }
}
